package com.example.textfielddemo.slice;

import com.example.textfielddemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.text.Font;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.text.Format;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取组件实例
        TextField textField = (TextField) findComponentById(ResourceTable.Id_text_field);
        // 设置输入框文字大小
        textField.setTextSize(30, Text.TextSizeType.VP);
        // 设置默认的提示文案
        textField.setHint("请输入内容");
        // 设置提示文案字体大小
        textField.setTextSize(30, Text.TextSizeType.VP);
        // 设置左内边距
        textField.setPaddingLeft(30);
        // 设置背景
        ShapeElement element = new ShapeElement();
        element.setCornerRadius(30); // 圆角
        element.setShaderType(ShapeElement.RECTANGLE); // 矩形
        element.setRgbColor(new RgbColor(00, 00, 255)); // 背景色
        textField.setBackground(element);
        // 添加输入框状态的监听
        textField.setFocusChangedListener(new Component.FocusChangedListener() {
            // 获取焦点或失去焦点时回调的方法
            @Override
            public void onFocusChange(Component component, boolean b) {
                ToastDialog toastDialog = new ToastDialog(getContext());
                toastDialog.setText(String.format("是否获取到焦点：%b", b));
                toastDialog.show();
            }
        });
        textField.setBubbleSize(30, 30);
        textField.setBasement(element);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
